package com.Test;

import com.itheima.Utils.MyBatisUtils;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.*;

import java.util.List;

public class StudentTest {

    private SqlSession sqlSession;
    private StudentMapper studentMapper;

    @BeforeEach
    public void setUp() {
        // 获取 SqlSession
        sqlSession = MyBatisUtils.getSession();
        studentMapper = sqlSession.getMapper(StudentMapper.class);
    }

    @AfterEach
    public void tearDown() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    @Test
    public void testSelectByName() {
        String sname = "Lucy"; // 输入需要查询的姓名
        List<Student> students = studentMapper.selectByConditions(sname, null);
        printStudents(students);
    }

    @Test
    public void testSelectByMajor() {
        String major = "计算机"; // 输入需要查询的专业
        List<Student> students = studentMapper.selectByConditions(null, major);
        printStudents(students);
    }

    @Test
    public void testSelectByNameAndMajor() {
        String sname = "Lili"; // 输入需要查询的姓名
        String major = "汽修"; // 输入需要查询的专业
        List<Student> students = studentMapper.selectByConditions(sname, major);
        printStudents(students);
    }
    @Test
    public void testSelectAllWithSidNotNull() {
        // 当姓名和专业都为空时，查询所有学号不为空的学生
        List<Student> students = studentMapper.selectByConditions(null, null);
        printStudents(students);
    }

    @Test
    public void testSelectByIdLessThan() {
        int id = 5; // 查询条件
        List<Student> students = studentMapper.selectByIdLessThan(id);

        printStudents(students);
    }


    private void printStudents(List<Student> students) {
        if (students != null && !students.isEmpty()) {
            System.out.println("查询到的学生信息: ");
            for (Student student : students) {
                System.out.println("ID: " + student.getSid() +
                        ", 姓名: " + student.getSname() +
                        ", 年龄: " + student.getSage() +
                        ", 专业: " + student.getMajor());
            }
        } else {
            System.out.println("没有找到符合条件的学生信息。");
        }
    }


}
